export default {
  namespaced: true,
  state: () => ({
    translations: {},
  }),
  mutations: {
    SET_TRANSLATIONS: (state, translations) => {
      state.translations = translations;
    },
  },
  actions: {
    setTranslations: ({ commit }, translations) => {
      commit("SET_TRANSLATIONS", translations);
    },
  },
  getters: {
    t: (state) => (key, params) => {
      let text = state.translations[key] || key;
      if (params && typeof params === 'object') {
        Object.keys(params).forEach(paramKey => {
          const placeholder = `${paramKey}`;
          text = text.replace(new RegExp(placeholder, 'g'), params[paramKey]);
        });
      }
      return text;
    },
  },
};
